package com.example.partner.activity.home;

import static com.example.partner.common.ConfigConstant.BASE_URL;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.partner.R;
import com.example.partner.adapter.user.UserAdapter;
import com.example.partner.common.BaseResponse;
import com.example.partner.model.domain.User;
import com.example.partner.service.FollowService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class InterestActivity extends AppCompatActivity {


    private FollowService followService;
    ListView listView;
    UserAdapter adapter;

    @Override
    public SharedPreferences getSharedPreferences(String name, int mode) {
        return super.getSharedPreferences(name, mode);
    }
    @SuppressLint({"MissingInflatedId", "NonConstantResourceId"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interest);
        listView = findViewById(R.id.userList);
        SharedPreferences sharedPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE);
        Long loginUserId = sharedPreferences.getLong("userId", -1);
        // 初始化Retrofit
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL) // 替换为您的服务器地址
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        followService = retrofit.create(FollowService.class);
        searchMyFollow(loginUserId);

        listView.setOnItemClickListener((adapterView, view, position, l) -> {
            // 获取点击的项
            User selectedUser = (User) adapter.getItem(position);
            String username = selectedUser.getUsername();
            String userTags = selectedUser.getTags();
            String userProfile = selectedUser.getProfile();
            String userPhone = selectedUser.getPhone();
            String userRole = String.valueOf(selectedUser.getUserRole());
            String avatarUrl = selectedUser.getAvatarUrl();
            // 创建Intent并启动新的Activity
            Intent intent = new Intent(InterestActivity.this, UserInfoActivity.class);
            intent.putExtra("username", username);
            intent.putExtra("userTags", userTags);
            intent.putExtra("userProfile", userProfile);
            intent.putExtra("userPhone", userPhone);
            intent.putExtra("avatarUrl",avatarUrl);
            intent.putExtra("userRole", userRole);
            startActivity(intent);
        });


    }


    private void searchMyFollow(Long loginUserId) {
        followService.searchMyFollow(loginUserId).enqueue(new Callback<BaseResponse<List<User>>>() {
            @Override
            public void onResponse(Call<BaseResponse<List<User>>> call, Response<BaseResponse<List<User>>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    // 处理成功的响应
                    List<User> users = response.body().getData();
                    if (users != null){
                        adapter = new UserAdapter(InterestActivity.this, users);
                        listView.setAdapter(adapter);
                    }

                } else {
                    // 处理错误情况
                }
            }

            @Override
            public void onFailure(Call<BaseResponse<List<User>>> call, Throwable t) {
                // 网络错误处理
                t.printStackTrace();
            }
        });
    }


}